import pymysql

select_field_list = ['userClass', 'idType', 'agentIdType',
                     'plateColor', 'vehClass', 'vehType', 'useCharacter',
                     'userType', 'obuStatus',
                     'cardType', 'cardStatus']
user_field = ['centerUserId', 'userClass', 'name', 'idType', 'idNo', 'phone', 'address', 'email', 'idPicUrl',
              'idPicReverseUrl', 'unitBank', 'unitBankAddress', 'unitBankAccount', 'unitTaxNumber', 'unitImage',
              'agentName', 'agentIdType', 'agentIdNum', 'agentAddress']
vehicle_field = ['centerUserId', 'plateNo', 'plateColor', 'vehClass', 'vehType', 'vehicleColor', 'vehicleModel',
                 'engineNo', 'vin', 'approvedCount', 'vehicleLength', 'vehicleWidth', 'vehicleHeight', 'ownerName',
                 'ownerAddress', 'vehicleType', 'useCharacter', 'registerDate', 'issueDate', 'fileNum', 'verifyRecord',
                 'totalMass', 'maintenanceMass', 'permittedWeight', 'permittedTowWeight', 'wheelCount', 'axleCount',
                 'axleDistance', 'licensePic1', 'licensePic2']
obu_field = ['centerUserId', 'obuId', 'obuMac', 'plateNo', 'plateColor', 'issuerFlag', 'obuVer', 'startTime', 'endTime',
             'userType', 'obuStatus', 'vehType', 'axleCount', 'wheelCount', 'weightOrSeat']
card_field = ['centerUserId', 'cardNet', 'cardId', 'plateNo', 'plateColor', 'issuerFlag', 'cardType', 'cardVer',
              'startTime', 'endTime', 'cardStatus', 'userType', 'vehType', 'userName', 'userIdType', 'userIdNum',
              'phone', 'signingBankId']
tag_field = {
    'user': user_field,
    'vehicle': vehicle_field,
    'obu': obu_field,
    'card': card_field,
}


def model_field(fields, line, tag):
    fields_list = tag_field[tag] if len(fields) < len(tag_field[tag]) else fields
    i = 0
    data_dict = {}
    for field in fields_list:
        i = i + 1
        if field in fields:
            data_dict[field] = str(line[i]) if field in select_field_list else line[i]
    return data_dict


def get_research_read(fields, sql, tag):
    conn = pymysql.connect(host='112.126.88.195', port=3306, user='zshr', password='zshr',
                           database='etc_issue_scgs')
    cursor = conn.cursor()

    cursor.execute(sql)
    data = []
    for line in cursor.fetchall():
        dict = model_field(fields, line, tag)
        dict["id"] = line[0]
        data.append(dict)
    cursor.close()
    conn.close()
    return data
